<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="usersLogin" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--<el-table-column prop="name" label="平台名称" width="200">-->
			<!--</el-table-column>-->
			<el-table-column prop="firstLoginRewardType" label="奖励道具" width="200">
			</el-table-column>
			<el-table-column prop="firstLoginRewardAmount" label="数量" width="200">
			</el-table-column>
			<el-table-column label="操作" min-width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="15" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<!--<el-form-item label="平台名称" prop="id">-->
					<!--<el-input v-model="editForm.name" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<el-form-item label="奖励道具" prop="prop">
					<el-input v-model="editForm.prop" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="数量" prop="number">
					<el-input v-model="editForm.number" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm"  label-width="80px" :rules="addFormRules" ref="addForm">
				<!--<el-form-item label="平台名称" prop="id">-->
					<!--<el-input v-model="editForm.name" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
				<el-form-item label="奖励道具" prop="prop">
					<el-input v-model="addForm.prop" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="数量" prop="number">
					<el-input v-model="addForm.number" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				usersLogin: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
                    number: [
                        { required: true, message: '请输入数量', trigger: 'blur' }
                    ],
                    prop: [
                        { required: true, message: '请输入道具名称', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {
					id: 0,
					name: '',
					sex: -1,
					age: 0,
					birth: '',
					addr: ''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    number: [
						{ required: true, message: '请输入数量', trigger: 'blur' }
					],
                    prop: [
                        { required: true, message: '请输入道具名称', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
                    number:'',
                    prop:''
				}

			}
		},
		methods: {
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				this.listLoading = true;
                this.$axios.get('/firstlogin-reward-configs',{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.total = res.data.count
                        this.usersLogin.push(res.data)
                        this.listLoading = false;
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
                    this.$axios.patch('/firstlogin-reward-configs',{},this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '删除成功', type: 'error'});
                            this.listLoading = false;
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					number:'',
					prop:''
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editUser(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {

			},
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>